<?php
class TeachersController extends AppController {

	var $name = 'Teachers';
	var $layout='teacher';
	
	function admin_index() {
		$this->Teacher->recursive = 0;
		$this->set('teachers', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid teacher', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('teacher', $this->Teacher->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Teacher->create();
			if ($this->Teacher->save($this->data)) {
				$this->Session->setFlash(__('The teacher has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The teacher could not be saved. Please, try again.', true));
			}
		}
		$accounts = $this->Teacher->Account->find('list');
		$managers = $this->Teacher->Manager->find('list');
		$profiles = $this->Teacher->Profile->find('list');
		$this->set(compact('accounts', 'managers', 'profiles'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid teacher', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Teacher->save($this->data)) {
				$this->Session->setFlash(__('The teacher has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The teacher could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Teacher->read(null, $id);
		}
		$accounts = $this->Teacher->Account->find('list');
		$managers = $this->Teacher->Manager->find('list');
		$profiles = $this->Teacher->Profile->find('list');
		$this->set(compact('accounts', 'managers', 'profiles'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for teacher', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Teacher->delete($id)) {
			$this->Session->setFlash(__('Teacher deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Teacher was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	/*
	 * @author tuan, do van

	 */
	function index (){
		$this->redirect('home');
	}
	function home ($id=null){		
			$this->set('teacher', $this->Teacher->read(null, $id));
			$this->set ('action','Home');
	}
	function test_list ($teacher_id){
			$teacher=$this->Teacher->read($teacher_id);
			$this->set('data', $teacher['Test']);
	}
	function scoring ($test_id){
		if (empty($this->data)){
			$this->loadModel('Test');		
			$this->set('data', $this->Test->read (null,$test_id));
		} else {
			$info=$this->data;
			$this->loadModel('Result');
			$this->Result->score($info);
			$this->Session->setFlash(__('Score done', true));
			$this->render();
		}
	}
}
